print "********************************************";
print "*                                          *";
print "*             TOSSIM Script                *";
print "*                                          *";
print "********************************************";


from TOSSIM import*
import sys
import random
t=Tossim([]);
numNodes = 100;
neighbours = [6,7,8,12,13,14];#lista utilizzata per fare delle simulazioni con un sottoinsieme dei nodi


for i in range(0, numNodes):
#for i in neighbours:
    node = t.getNode(i)	
    node.bootAtTime((15 + t.ticksPerSecond() / 10) * i + 1)
   #node.bootAtTime(random.random()*51)
   #node.bootAtTime(0)

r=t.radio();

#Crea un link da sorgente a destinazione tenendo conto del gain
gain = open("linkgain100.out", "r")
lines = gain.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
    if (s[0] == "gain"):
       r.add(int(s[1]), int(s[2]), float(s[3]))

#Parte errata!
#Creazione del modello di rumore
#noise = open("linkgain100.out", "r")
#lines = noise.readlines()
#for line in lines:
#    s = line.split()
#    if (len(s) > 0):
#	if (s[0] == "noise"):
#	    t.getNode(int(s[1])).addNoiseTraceReading(int(float(s[2])))
#	    t.getNode(int(s[1])).addNoiseTraceReading(int(float(s[3])))
#for i in range(0, numNodes):
#    t.getNode(i).createNoiseModel()
#Parte errata!

#Creazione del modello di rumore
modelfile="meyer-heavy-reduced.txt";
noise = open(modelfile, "r")
lines = noise.readlines()
for line in lines:
    str = line.strip()
    if (str != ""):
        val = int(str)
        for i in range(0, numNodes): #range(1,n): numeri da 1 a n-1 compreso
            t.getNode(i).addNoiseTraceReading(val)
for i in range(0, numNodes):
    t.getNode(i).createNoiseModel()

t.addChannel("boot",sys.stdout);
t.addChannel("radio",sys.stdout);
t.addChannel("tx",sys.stdout);
t.addChannel("rx",sys.stdout);


print "Simulation started..."

for i in range(0,100000):
	t.runNextEvent()

print "Simulation finished!"
